package com.artron.mediaartron.data.task;

import android.content.Context;
import android.util.Xml;

import com.artron.mediaartron.data.entity.Address;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-23.
 */

public class AddressTask {

    private AddressTask(){}

    public static void getAddress(final Context context, Action1<Address> action1){
        Observable.create(new Observable.OnSubscribe<Address>() {
            @Override
            public void call(Subscriber<? super Address> subscriber) {
                subscriber.onStart();
                Address address = new Address();
                try {
                    InputStream open = context.getAssets().open("address.xml");
                    XmlPullParser parser = Xml.newPullParser();
                    parser.setInput(open, "utf-8");
                    Address.Province province = null;
                    Address.City city = null;
                    Address.District district = null;

                    int arrayLevel = 0;
                    int dictLevel = 0;
                    boolean isType = false;
                    String type = "";
                    loop:while (true) {
                        int next = parser.next();
                        String name = parser.getName();
                        switch (next) {
                            case XmlPullParser.START_DOCUMENT:
                                break;
                            case XmlPullParser.START_TAG:
                                if ("array".equals(name)) {
                                    arrayLevel++;
                                }

                                if ("dict".equals(name)) {
                                    dictLevel++;
                                }

                                if ("key".equals(name)) {
                                    isType = true;
                                }

                                if ("string".equals(name) || "integer".equals(name)) {
                                    isType = false;
                                }
                                break;
                            case XmlPullParser.TEXT:
                                if (parser.getText().contains("\n") || parser.getText().contains("\t")) {
                                    break;
                                }

                                if (isType) {
                                    type = parser.getText();
                                    if (arrayLevel == 1 && dictLevel == 1) {
                                        if ("name".equals(parser.getText())) {
                                            province = new Address.Province();
                                            address.addItem(province);
                                        }
                                    } else if (arrayLevel == 2 && dictLevel == 2) {
                                        if ("name".equals(parser.getText())) {
                                            city = new Address.City();
                                            province.addItem(city);
                                        }
                                    } else if (arrayLevel == 3 && dictLevel == 3) {
                                        if ("name".equals(parser.getText())) {
                                            district = new Address.District();
                                            city.addItem(district);
                                        }
                                    }
                                } else {
                                    if (arrayLevel == 1 && dictLevel == 1) {
                                        province.set(type, parser.getText());
                                    } else if (arrayLevel == 2 && dictLevel == 2) {
                                        city.set(type, parser.getText());
                                    } else if (arrayLevel == 3 && dictLevel == 3) {
                                        district.set(type, parser.getText());
                                    }
                                }

                                break;
                            case XmlPullParser.END_TAG:
                                if ("array".equals(name)) {
                                    arrayLevel--;
                                }

                                if ("dict".equals(name)) {
                                    dictLevel--;
                                }

                                if ("key".equals(name)) {
                                    isType = false;
                                }
                                break;
                            case XmlPullParser.END_DOCUMENT:
                                break loop;
                        }
                    }
                } catch (IOException | XmlPullParserException e) {
                    e.printStackTrace();
                }

                subscriber.onNext(address);
            }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(action1);
    }

    public static List<Address.Province> getProvinces(Address address) {
        return address.getProvinces();
    }

    public static List<List<Address.City>> getCities(List<Address.Province> provinces) {
        List<List<Address.City>> cities = new ArrayList<>();
        for (int i = 0; i < provinces.size(); i++) {
            cities.add(provinces.get(i).getCityList());
        }
        return cities;
    }

    public static List<List<List<Address.District>>> getDistricts(List<Address.Province> provinces) {
        List<List<List<Address.District>>> districts = new ArrayList<>();
        for (Address.Province item : provinces) {
            List<List<Address.District>> list = new ArrayList<>();
            districts.add(list);

            for (Address.City city : item.getCityList()) {
                list.add(city.getDistricts());
            }
        }
        return districts;
    }
}
